﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Frame.Lib.Core.User;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Dept;
using Frame.Lib.Enums;
using Frame.Lib.Core;

public partial class Admin_User_Edit : PageEdit
{
    UserBO user
    {
        get
        {
            return UserBO.Instance<UserBO>();
        }
    }
    private string UserID
    {
        get
        {
            if (!IsPostBack)
            {
                if (Request["ID"] != null)
                {
                    ViewState["ID"] = StrUtil.DecryptDES(Request["ID"]);
                }
            }
            return (string)ViewState["ID"];
        }
        set
        {
            ViewState["ID"] = value;
        }
    }
    private string DeptID
    {
        get
        {
            return Request["DeptID"];
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            if (UserID != null && UserID != "")
            {
                DispInfo();
            }
            else
            {
                Orders.Text = user.GetMaxOrder().ToString();
                txtDeptID.Value = DeptID;
                txtDeptName.Text = (new DeptBO()).GetName(DeptID);               
            }
            WebUtil.SetSaveBtn(Request["ID"], btnSave);
        }
        AddAttr();
    }

    private void AddAttr()
    {
        JSUtil.ShowTreeDlg(txtDeptID, txtDeptName, STableName.Dept, "选择部门", false);
    }

    private void DispInfo()
    {
        UserInfo info = user.GetInfo((object)UserID);
        EntityUtil.SetControl(pnlMain, info);

        txtPassword.Attributes.Add("value",StrUtil.EncryptMD5(info.Password));
        hidPassword.Value = StrUtil.EncryptMD5(info.Password);
        txtDeptID.Value = StrUtil.MergeToStr(info.DeptIDs, ",");        
        txtDeptName.Text = info.DeptName;
    }

    private void ClearInfo()
    {
        UserInfo info = new UserInfo();
        
        UserID = null;

        Name.Text = "";
        LoginName.Text = "";
        Orders.Text = user.GetMaxOrder().ToString();
       
        txtPassword.Attributes["value"] = "";
        hidPassword.Value = "";
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strLoginName = LoginName.Text.Trim();

        if (strLoginName == Constants.ADMIN_NAME)
        {
            base.Alert("该用户名为系统保留字，用不使用");
            return;
        }

        bool blnFlag = user.ExistField(TableField.LoginName,strLoginName, UserID);
        if (blnFlag)
        {
            JSUtil.Alert("该登录名已存在");
        }
        else
        {
            UserInfo info = user.GetInfo((object)UserID);

            EntityUtil.SetEntity(pnlMain, info);
            info.DeptIDs = StrUtil.SplitToInt(txtDeptID.Value);
            if (UserID == null)
            {
                info.Status = (int)UserStatus.启用;
            }

            if (UserID == null)//新增
            {
                info.Password = txtPassword.Text;
            }
            else
            {
                if (txtPassword.Text.Trim() != hidPassword.Value)
                {
                    info.Password = txtPassword.Text;
                }
            }

            user.Save(info);


            if (UserID == null)
            {
                JSUtil.RefreshView(this);
                ClearInfo();
            }
            else
            {
                JSUtil.RefreshView();
            }
        }
    }
}
